/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.SeverityCodeDAO;
import exatrez.manager.SeverityManager;
import exatrez.model.InfoAudit;
import exatrez.model.SeverityCode;
import exatrez.model.User;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.config.AuthRequired;
import java.util.Date;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author wan.fairul
 */
@UrlBinding("/adminSeverity")
@AuthRequired
public class SeverityAction extends BaseActionBean {

    private static Logger LOG = LoggerFactory.getLogger(SeverityAction.class);
    private SeverityCode severity;
    @Inject
    private SeverityManager severityManager;
    @Inject
    private SeverityCodeDAO severityCodeDAO;

    @DefaultHandler
    public Resolution add_severity() {
        severity = new SeverityCode();
        return new JSP("admin/severity.jsp");
    }

    public Resolution saveSeverity() {
        SeverityCode s = severityManager.findSeverityName(severity.getSeverityName());
        LOG.debug("masuk");
        if (s == null) {
            User user = getContext().getCurrentUser();
            InfoAudit ia = new InfoAudit();
            ia.setCreateDate(new Date());
            ia.setCreateId(user);
            severity.setInfoAudit(ia);
            severityManager.saveOrUpdate(severity);
            addSimpleMessage("Success");
            LOG.debug("finish");
        } else {
            addSimpleError("Duplicate Severity!");
        }
        return add_severity();
    }

    public Resolution deleteSeverity() {
        String severityCodeId = getContext().getRequest().getParameter("severityCodeId");
        if (severityCodeId != null) {
            severity = severityCodeDAO.findById(Integer.parseInt(severityCodeId));
            if (severity != null) {
                severityManager.deleteSeverity(severity);
                addSimpleMessage("Success Delete");
            }
        }
        return new RedirectResolution(SeverityAction.class, "add_severity");
    }

    public Resolution editSeverity() {
        String severityCodeId = getContext().getRequest().getParameter("severityCodeId");
        if (severityCodeId != null) {
            severity = severityCodeDAO.findById(Integer.parseInt(severityCodeId));
        }
        return new JSP("admin/edit_severity.jsp").addParameter("popup", true);
    }

    public Resolution saveEditSeverity() {
        String severityCodeId = getContext().getRequest().getParameter("severity.severityCodeId");
        if (severityCodeId != null) {
            User user = getContext().getCurrentUser();
            SeverityCode s = severityCodeDAO.findById(Integer.parseInt(severityCodeId));
            if (!severity.getSeverityName().equals(s.getSeverityName())) {
                SeverityCode sc = severityManager.findNotSeverityName(String.valueOf(s.getSeverityCodeId()), severity.getSeverityName());
                if (sc != null) {
                    addSimpleError("Duplicate Severity Name!");
                    return add_severity();
                } else {
                    s.setSeverityName(severity.getSeverityName());
                }
            }
            InfoAudit ia = s.getInfoAudit();
            s.setSeverityDay(severity.getSeverityDay());
            s.setSeverityColor(severity.getSeverityColor());
            ia.setUpdateDate(new Date());
            ia.setUpdateId(user);
            s.setInfoAudit(ia);
            severityManager.saveOrUpdate(s);
            addSimpleMessage("Success Update");
        }
        return new RedirectResolution(SeverityAction.class, "add_severity");
    }

    public SeverityCodeDAO getSeverityCodeDAO() {
        return severityCodeDAO;
    }

    public void setSeverityCodeDAO(SeverityCodeDAO severityCodeDAO) {
        this.severityCodeDAO = severityCodeDAO;
    }

    public SeverityCode getSeverity() {
        return severity;
    }

    public void setSeverity(SeverityCode severity) {
        this.severity = severity;
    }

    public SeverityManager getSeverityManager() {
        return severityManager;
    }

    public void setSeverityManager(SeverityManager severityManager) {
        this.severityManager = severityManager;
    }
}
